package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.Department;
import com.bwhx.eruler.bean.Menus;
import com.bwhx.eruler.service.DepartmentService;
import com.bwhx.eruler.service.MenusService;
/**
 * 部门管理
 * @author Cuilitang
 *
 */

@Controller
public class MenusController extends BaseController {
	
	@Autowired
	MenusService menusService;
	
	@ResponseBody
	@RequestMapping(value = "/page.menusList.ashx")
	 public List<Menus> menusList(Integer mType) {
		//每页的开始记录
        
		List<Menus> list = menusService.listAll(mType);
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.staffMenusList.ashx")
	 public List<Menus> SessionStaffMenusList(HttpSession session, Integer mType) {
		//每页的开始记录
		Staff staff = (Staff) session.getAttribute("staff");
		List<String> idsList= new ArrayList<>();
		if(staff!=null){
			if(staff.getsLoginName().equals("admin")){
				List<Menus> menusList = menusList(mType);
				return menusList;
			}
			String ids = staff.getMenuIds();
			if(ids!=null&&ids.length()!=0){
				String[] idsArr = ids.split(",");
				idsList = Arrays.asList(idsArr);
			}else{
				ArrayList<Menus> arrayList = new ArrayList<Menus>();
				Menus menus = new Menus();
				menus.setmName("没有权限");
				menus.setmHref("page.401.ashx");
				menus.setmType(0);
				arrayList.add(menus);
				return arrayList;
			}
			
					
					
		}
		
		List<Menus> list = menusService.listByStaff(idsList,mType);
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.sessionStaffAndChoosenStaffMenusList.ashx")
 	 public List<Menus> SessionStaffAndChoosenStaffMenusList(HttpSession session,String menuIds, Integer mType) {
		
		Staff sessionStaff = this.getSessionStaff();
		 String ids = sessionStaff.getMenuIds();
		 List<String> idsList= new ArrayList<>();
		 String[] idsArr = ids.split(",");
		 idsList.addAll(Arrays.asList(idsArr));
		 
		 String[] paramIds = menuIds.split(",");
		 idsList.addAll(Arrays.asList(paramIds)) ;
		
		List<Menus> list = menusService.listByStaff(idsList,mType);
		
	    return list;  
	  } 
	



	

}
